//
//  YXMainViewController.swift
//  微博YX
//
//  Created by apple on 15/11/23.
//  Copyright © 2015年 lyx. All rights reserved.
//

import UIKit

class YXMainViewController: UITabBarController {

    //监听中间加号按钮点击事件
    func composeClick(){
        print(__FUNCTION__)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        
//        
////        添加自定义的tabbar
//        let newtabBar = YXTabBar()
//////        kvc
////        setValue(tabBar, forKey: "tabbar")
//        newtabBar.composeButton.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchDragInside)
        
        
        tabBar.tintColor = UIColor.orangeColor()
//      添加控制器
//        首页
        let homeVC = YXHomeViewController()
         addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
//        消息
        let messageVC = YXMessageViewController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
//        中间加号控制器(占位)
        addChildViewController(UIViewController())
        
//        我
        let profileVC = YXProfileViewController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
//        发现
        let discoverVC = YXDiscoverViewController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
    }
 
    /**
    自定义添加tabbar子控制器
    
    - parameter controller: 子控制器
    - parameter title:      子控制器的标题
    - parameter imageName:  子控制器的图片名称
    */
    // private 表示只有本类或当前文件的其他类可以访问
    private  func addChildViewController(controller: UIViewController , title : String , imageName : String) {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        // 拼接高亮图片名称
        let highlightedImageName = imageName + "_highlighted"
        // 让选中的图片使用原来的颜色,不要系统帮我们渲染
        let highlightedImage = UIImage(named: highlightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        // 设置高亮图片
        controller.tabBarItem.selectedImage = highlightedImage
        
        
//        设置导航控制器的根控制器
        addChildViewController(UINavigationController(rootViewController: controller))
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
//        设置加号按钮的frame
        let  width = tabBar.bounds.size.width / 5
        let frame = CGRect(x: 2 * width - 5, y: 0, width: width + 10 , height: tabBar.bounds.height)
        
        composeButton.frame = frame
        
    }
    //    懒加载创建中间的按钮
    lazy var composeButton: UIButton = {
        
        let button = UIButton()
        //       设置图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(button)
        // 添加点击事件
        button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)

        return button
        }()

    
    
    
    
    
}
